var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var origin = null;
var destination = null;
var waypoints = [];
var markers = [];
var directionsVisible = false;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(31.86, 117.28);
    var myOptions = {
	zoom:15,
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    
    google.maps.event.addListener(map, 'click', function(event) {
	if (origin == null) {
            origin = event.latLng;
            addMarker(origin);
	} else if (destination == null) {
            destination = event.latLng;
            addMarker(destination);
	} else {
            if (waypoints.length < 9) { // if we don't set limitation, the DirectionService may return after long period.
		waypoints.push({ location: destination, stopover: true });
		destination = event.latLng;
		addMarker(destination);
            } else {
		alert("Maximum number of waypoints reached");
            }
	}
    });
}

function addMarker(latlng) {
    Station station = new Station(latlng, "Test", "12.02");
    station.setMap(map);
    station.setIcon("http://maps.google.com/mapfiles/marker" + String.fromCharCode(markers.length + 65) + ".png");
    markers.push(station);
    //markers.push(new google.maps.Marker({
//	position: latlng, 
//	map: map,
//	icon: "http://maps.google.com/mapfiles/marker" + String.fromCharCode(markers.length + 65) + ".png"
  //  }));    
}

function calcRoute() {
    if (origin == null) {
	alert("Click on the map to add a start point");
	return;
    }
    
    if (destination == null) {
	alert("Click on the map to add an end point");
	return;
    }
    mode = google.maps.DirectionsTravelMode.DRIVING;
    var request = {
        origin: origin,
        destination: destination,
        waypoints: waypoints,
        travelMode: mode,
        optimizeWaypoints: true,
        avoidHighways: document.getElementById('highways').checked,
        avoidTolls: document.getElementById('tolls').checked
    };
    
    directionsService.route(request, function(response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
	}
    });
    
    clearMarkers();
    directionsVisible = true;
}
/*
function updateMode() {	
    if (directionsVisible) {
	calcRoute();
    }
}
*/

function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
	markers[i].setMap(null);
    }
}

function clearWaypoints() {
    markers = [];
    origin = null;
    destination = null;
    waypoints = [];
    directionsVisible = false;
}

function reset() {
    clearMarkers();
    clearWaypoints();
    directionsDisplay.setMap(null);
    directionsDisplay.setPanel(null);
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));    
}

function save() {
    jQuery('div#stations').text("Save successfully.");
}